const util = require('../../utils/util')
const InfoApi = require('../../api/information.js')
const QuoteApi = require('../../api/quote.js')

Page({
  data: {
    swiperlist: [],
    menuList: [],
    linkList: [],
  },

  onShow: function () {
    if (typeof this.getTabBar === 'function' &&  this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
    this.getFrontPageLocationList()
  },
  // 查询前端页面位置配置
  getFrontPageLocationList(){
    InfoApi.getFrontPageLocationList({pageId: '1713818862842454017'}).then(res=>{
      if (res.retCode == '000000' && res.errCode == '000000') {
        res.data.forEach(item=>{
          if (item.componentType == 'carousel_chart') {
            this.getFrontCarouselChartList('1713818862842454017', item.locationId);
          } else if (item.componentType == 'menu_group') {
            this.getFrontMenuGroupList('1713818862842454017', item.locationId);
          } else if (item.componentType == 'text_link') {
            this.getFrontTextLinkList('1713818862842454017', item.locationId);
          }
        })
      }
    })
  },
  // 查询前端轮播图组件
  getFrontCarouselChartList(pageId, locationId){
    InfoApi.getFrontCarouselChartList({pageId: pageId, locationId: locationId}).then(res=>{
      if (res.retCode == '000000' && res.errCode == '000000') {
        this.setData({
          swiperlist: res.data[0].list
        })
      }
    })
  },
  // 前端查询菜单云组件
  getFrontMenuGroupList(pageId, locationId){
    InfoApi.getFrontMenuGroupList({pageId: pageId, locationId: locationId}).then(res=>{
      if (res.retCode == '000000' && res.errCode == '000000') {
        wx.setStorageSync('menuGroupList', res.data[0].itemList)
        this.setData({
          menuList: res.data[0].itemList
        })
      }
    })
  },
  // 查询前端链接组件
  getFrontTextLinkList(pageId, locationId){
    InfoApi.getFrontTextLinkList({pageId: pageId, locationId: locationId}).then(res=>{
      if (res.retCode == '000000' && res.errCode == '000000') {
        let array = [];
        if (res.data && res.data.length > 0) {
          if (res.data[0].list && res.data[0].list.length > 0) {
            array = res.data[0].list.map(i=>{
              if (i.publishTime) {
                i.publishTime = util.getColumnDateDiff(i.publishTime)
              }
              return i;
            })
          }
        }
        this.setData({
          linkList: array
        })
      }
    })
  },
  // 前端菜单查询
  getFrontMenuList(_menuId){
    QuoteApi.getFrontMenuList({menuId: _menuId}).then(res=>{
      if (res.retCode == '000000' && res.errCode == '000000') {
        wx.setStorageSync('currentMenuItemList', res.data)
      }
    })
  },
  // 查看链接详情
  checkDetail(e){
    let currentItem = e.currentTarget.dataset.item;
    wx.setStorageSync('newsInfo', currentItem)
    wx.navigateTo({
      url: '/packageA/pages/newsDetail/index',
    })
  },
  // 查看菜单详情
  checkMenuItem(e){
    let currentItem = e.currentTarget.dataset.item;
    if (currentItem.menuId == '1531168972242444290') { // 数据
      wx.navigateTo({ url: '/packageA/pages/data/index' })
    } else if (currentItem.menuId == '0') { // 资讯
      wx.switchTab({ url: '/pages/news/index' })
    } else if (currentItem.menuType == 'general') { // 报价页
      this.getFrontMenuList(currentItem.menuId);
      console.log(currentItem);
      wx.setStorageSync('currentMenuItem', currentItem);
      setTimeout(()=>{
        wx.navigateTo({
          url: '/packageA/pages/quotation/index',
        })
      }, 500);
    }
  },
})
